: 2defineString create 0 , 0 , ;

\ ******************Variables and Values ************************

\ Max buffer length
1 Value maxBuffer

\ Bytes read
0 Value bytesRead

\ End of File marker
0 Value EOF

Create lineBuffer maxBuffer allot

\ Create our variables, fileInput and fileOutput
2defineString fileInput
2defineString fileOutput

\ We need file descriptors for the input and output
0 Value fdInput
0 Value fdOutput

\ **************************Words********************************

\ Set the maxBuffer to a new value greater than 0
: SET_BUFFER ( n -- ) dup
	0 > IF
		to maxBuffer
	ELSE
		drop
	ENDIF ;


\ Reads a line from fdInput to the buffer
: READ_LINE ( -- ) lineBuffer maxBuffer fdInput read-line throw
	to EOF
	to bytesRead ;
	
\ Writes a line from the buffer to fdOutput
: WRITE_LINE ( -- ) lineBuffer bytesRead fdOutput write-file throw ;
	
\ Prints the buffer
: PRINT_BUFFER ( -- ) lineBuffer bytesRead type ;

\ Input and Output store words for ease of use
: INPUT_STORE ( " -- ) fileInput 2! ;
: OUTPUT_STORE ( " -- ) fileOutput 2! ;

\ Input and Output read words for ease of use
: INPUT_READ ( -- a- n )fileInput 2@ ;
: OUTPUT_READ ( -- a- n) fileOutput 2@ ;

\ Opens up the file descriptor described by fileInput/fileOutput and stores
\ In fdInput/fdOuput
: INPUT_OPEN ( -- ) INPUT_READ r/o open-file throw to fdInput ;
: OUTPUT_OPEN ( -- ) OUTPUT_READ w/o create-file throw to fdOutput ;

: INPUT ( " -- ) INPUT_STORE INPUT_OPEN ;
: OUTPUT ( " -- ) OUTPUT_STORE OUTPUT_OPEN ;

: INPUT_CLOSE ( -- ) fdInput close-file throw ;
: OUTPUT_CLOSE ( -- ) fdOutput close-file throw ;